package com.supermap.dataservice.cloud.key;

import org.apache.ibatis.annotations.*;
import org.apache.logging.log4j.util.Strings;

import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @desc
 * @date 2019年07月19 14:30
 * @email 1301584033@qq.com
 */
@Mapper
public interface KeyWordMapper {

    @Select("select realname from t_s_base_user where id=#{userId}")
    String getUserName(@Param("userId")String userId);

    @Update("update sys_key_word set state=#{state},auditorContent=#{autoContent},auditor=#{auditor},auditorId=#{auditorId} where id=#{keyId}")
    Integer upState(@Param("keyId")Integer keyId,@Param("state") Integer state,@Param("autoContent") String autoContent,@Param("auditor")String auditor,@Param("auditorId")String auditorId);

    @Insert("insert into sys_key_word(userId,typeCode,typeId,typeTitle,content,keyword,keygroup,`datetime`) " +
            " values (#{userId},#{typeCode},#{typeId},#{typeTitle},#{content},#{keyword},#{keygroup},#{datetime})")
    Integer putKeyWord(@Param("userId")Integer userId,@Param("typeCode")String typeCode,@Param("typeId")Integer typeId,@Param("typeTitle")String typeTitle,@Param("content")String content,@Param("keyword")String keyword,@Param("keygroup")String keygroup,@Param("datetime")String datetime);

    @SelectProvider(type = KeyWordMapperSQL.class,method = "getSysKeyWordListSQL")
    List<Map<String,Object>> getSysKeyWordList(@Param("areaCode") String areaCode, @Param("typeCode")String typeCode,@Param("state") Integer state);


    @Select("update sys_key_word set del_flg=1 where id=#{keyId}")
    Integer delKeyWord(@Param("keyId") Integer keyId);

    @Select("select group_concat(DISTINCT SENSITIVETYPE) as keygroup,group_concat(DISTINCT SENSITIVEWORDS) as keyword from alert_info_sensitive_word_library where   locate(SENSITIVEWORDS,#{str})>0")
    Map<String,Object> getKeyWord(@Param("str")String str);


    @Select("SELECT LAST_INSERT_ID()")
    Integer getLastId();

    //农情
    @Update("update t_agrinfo set del_flg=#{del} where id=#{id}")
    Integer delNq(@Param("id")Integer id,@Param("del")Integer del);
    //农情评论
    @Update("update t_agrinfocomment set del_flg=#{del} where id=#{id}")
    Integer delNqHd(@Param("id")Integer id,@Param("del")Integer del);

    //咨询
    @Update("update t_advisoryinfo set del_flg=#{del} where id=#{id}")
    Integer delZx(@Param("id")Integer id,@Param("del")Integer del);
    //咨询互动
    @Update("update t_advinfocomment set del_flg=#{del} where id=#{id}")
    Integer delZxHd(@Param("id")Integer id,@Param("del")Integer del);
    //灾情
    @Update("update t_disaster set del_flg=#{del} where id=#{id}")
    Integer delZq(@Param("id")Integer id,@Param("del")Integer del);
    //灾情互动
    @Update("update t_disastercomment set del_flg=#{del} where id=#{id}")
    Integer delZqHd(@Param("id")Integer id,@Param("del")Integer del);
    //建议
    @Update("update t_suggest set del_flg=#{del} where id=#{id}")
    Integer delJy(@Param("id")Integer id,@Param("del")Integer del);


    class  KeyWordMapperSQL{
            public String getSysKeyWordListSQL(Map<String,Object> map){
                String areaCode=(String)map.get("areaCode");
                String typeCode=(String)map.get("typeCode");
                Integer state=(Integer)map.get("state");
                StringBuilder stringBuilder=new StringBuilder();
                stringBuilder.append("select t1.*,left(t1.datetime,10) as leftdate,t2.showName,t2.loginName from sys_key_word t1\n" +
                        "inner join t_user t2\n" +
                        "on t1.userId=t2.id\n" +
                        "and t1.del_flg=0 ");
                if(Strings.isNotBlank(areaCode)){
                    stringBuilder.append(" and t2.areaCode like concat('"+areaCode+"','%') ");
                }
                if(Strings.isNotBlank(typeCode)){
                    stringBuilder.append(" and t1.typeCode ='"+typeCode+"' ");
                }
                if(state!=null){
                    stringBuilder.append(" and t1.state ="+state+" ");
                }
                stringBuilder.append( " order by  t1.datetime desc ");
                return  stringBuilder.toString();
            }

    }


}
